﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using AjaxControlToolkit.HTMLEditor;
using spk_eAuction.Classes;
using FredCK.FCKeditorV2;
using System.Web.Security;
namespace spk_eAuction.admin
{
    public partial class ManageProducts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["cmd"] != null)
            {
                MultiView1.SetActiveView(View2);
                string cmd = Request.QueryString["cmd"].ToString();
                if (cmd == "insert")
                {
                    LableView2Head.Text = "Thêm sản phẩm";
                    DetailsView1.ChangeMode(DetailsViewMode.Insert);
  
                    Label LblUser = DetailsView1.FindControl("LblUser") as Label;
                    if (LblUser != null && Membership.GetUser().IsOnline)
                        LblUser.Text = Membership.GetUser().ProviderUserKey.ToString();
                }
                else if(cmd == "edit")
                {
                    LableView2Head.Text = "Sửa đổi thông tin sản phẩm";
                    DetailsView1.ChangeMode(DetailsViewMode.Edit);
                }
                else if (cmd == "view")
                {
                    LableView2Head.Text = "Chi tiết sản phẩm";
                    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                }
            }
            else
            {
                MultiView1.SetActiveView(View1);
            }
        }

        protected void BtnUpload1_Click(object sender, EventArgs e)
        {
            FileUpload FileUpload1 = DetailsView1.FindControl("FileUpload1") as FileUpload;
            Image Image1 = DetailsView1.FindControl("Image1") as Image;

            if (FileUpload1 != null && FileUpload1.HasFile)
            {
                string fileName = Server.MapPath(@"\images\products\" + FileUpload1.FileName);
                string imgUrl = @"~\images\products\" + FileUpload1.FileName;
                FileUpload1.SaveAs(fileName);
                if (Image1 != null)
                    Image1.ImageUrl = imgUrl;
            }
        }

        protected void BtnUpload2_Click(object sender, EventArgs e)
        {
            FileUpload FileUpload2 = DetailsView1.FindControl("FileUpload2") as FileUpload;
            Image Image2 = DetailsView1.FindControl("Image2") as Image;

            if (FileUpload2 != null && FileUpload2.HasFile)
            {
                string fileName = Server.MapPath(@"\images\products\" + FileUpload2.FileName);
                string imgUrl = @"~\images\products\" + FileUpload2.FileName;
                FileUpload2.SaveAs(fileName);
                if (Image2 != null)
                    Image2.ImageUrl = imgUrl;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            /* 
             * Eval("Details").ToString().Substring(0, (Eval("Details").ToString().Length > 20 ? 20 : Eval("Details").ToString().Length)) + "..." 
             */
            TableCell cellDetails = e.Row.Cells[e.Row.Cells.Count - 1];
            Label Label7 = cellDetails.FindControl("Label7") as Label;
            if (Label7 != null )
            {
                
                Label7.Text = Label7.ToolTip = TextConvertor.Html2Text(Label7.Text);
                if (Label7.Text.Length > 20)
                    Label7.Text = Label7.Text.Substring(0, 20) + "...";
            }
            
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            TextBox TextBox8 = DetailsView1.FindControl("TextBox8") as TextBox;
            Image Image1 = DetailsView1.FindControl("Image1") as Image;
            if (TextBox8 != null && Image1 != null)
            {
                Image1.ImageUrl = TextBox8.Text;
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            TextBox TextBox9 = DetailsView1.FindControl("TextBox9") as TextBox;
            Image Image2 = DetailsView1.FindControl("Image2") as Image;
            if (TextBox9 != null && Image2 != null)
            {
                Image2.ImageUrl = TextBox9.Text;
            }
        }
    }
}